import { ref } from 'vue'

export default () => {
  const teams = ref<TeamModel[]>([])
  const team = ref<TeamModel>()

  const all = async (args: Record<string, any> = {}) => {
    // const { page } = args
    // const row = 10
    // const start = (page - 1) * row + 1
    // const end = page * row
    // console.log(await window.api.sql(`select count(*) from teams`, 'count'))
    // return {
    //   data: await window.api.sql(
    //     `select * from teams order by compId desc limit ${start},${end} `,
    //     'findAll'
    //   ),
    //   row,
    //   total: await window.api.sql(`select count(name) from teams`, 'count')
    // }
    // teams.value = await window.api.sql(
    //   `select * from teams order by compId desc limit ${start},${end} `,
    //   'findAll'
    // )
    teams.value = await window.api.sql(`select * from teams order by compId desc `, 'findAll')
  }

  const add = async (data: TeamModel) => {
    return await window.api.sql<number>(
      `insert into teams (name,fzr,compId,evaluate,created_at) values (@name,@fzr,@compId,@evaluate,datetime())`,
      'insert',
      {
        name: data.name,
        fzr: data.fzr,
        compId: data.compId,
        evaluate: data.evaluate
      }
    )
  }

  const find = async (id: number) => {
    team.value = await window.api.sql(`select * from teams where id=@id`, 'findOne', { id })
  }

  const update = async (data: TeamModel) => {
    return await window.api.sql(
      `update teams set compId=@compId,name=@name,fzr=@fzr,evaluate=@evaluate where id=@id`,
      'update',
      {
        id: data.id,
        compId: data.compId,
        name: data.name,
        fzr: data.fzr,
        evaluate: data.evaluate
      }
    )
  }

  return { all, find, add, update, teams, team }
}
